//
//  CKOrderStatusAndDetailVC.m
//  FreshBee
//
//  Created by chenkai on 16/10/16.
//  Copyright © 2016年 iOS013. All rights reserved.
//

#import "CKOrderStatusAndDetailVC.h"
#import "CKOrderStatusCell.h"
#import "CKOrderModel.h"
#import "CKStatusTimeline.h"
#import "CKOrderDetilCell.h"
#import "CKShouHuoCell.h"
#import "CKSystemMessageCell.h"

@interface CKOrderStatusAndDetailVC ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,weak)UITableView *tableView;
@property (nonatomic,weak)UITableView *tableView2;
@property (nonatomic,weak)UIView *bottomView;
@property (nonatomic,weak)UISegmentedControl *myTitleView;


@property (nonatomic,weak)UITableView *tableView3;
@property (nonatomic,weak)UIView *tableView4;
//@property (nonatomic,assign)NSInteger section;

@property (nonatomic,assign)BOOL isNeedReload;

@property (nonatomic,strong)NSMutableArray *heightArray;
@property (nonatomic,weak)CKSystemMessageCell *lastCell;
@end

NSString *statusReuseID = @"statusReuseID";
NSString *detailReuseID = @"detailReuseID";
NSString *shouHuoReuseID = @"shouHuoReuseID";

NSString *systemMessageCellID = @"type1";



@implementation CKOrderStatusAndDetailVC

- (instancetype)initWithViewType:(NSInteger)viewType{
    if(self = [super init]){
        self.viewType = viewType;
    }
    return self;
}

- (NSArray *)heightArray{
    if(!_heightArray){
        _heightArray = [NSMutableArray arrayWithObjects:@0,@0,@0,@0,@0, nil];
    }
    return _heightArray;
}

#pragma mark
#pragma mark 懒加载子控件:
- (UIView *)tableView4{
    if(!_tableView4){
        UIView *view = [[UIView alloc]init];
        _tableView4 = view;
        [view setBackgroundColor:[UIColor lightGrayColor]];
        [self.view addSubview:view];
        
        }
    return _tableView4;
}


- (UITableView *)tableView3{
    if(!_tableView3){
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView3 = tableView;
        [self.view addSubview:_tableView3];
        [tableView setDelegate:self];
        [tableView setDataSource:self];
        tableView.tag = 30;
        tableView.separatorStyle = UITableViewCellSelectionStyleNone;
        [tableView registerNib:[UINib nibWithNibName:@"CKSystemMessageCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:systemMessageCellID];
        tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.1)];
        tableView.sectionHeaderHeight = 0;
        tableView.sectionFooterHeight = 20;
    }
    return _tableView3;
}



//我的订单
- (UITableView *)tableView{
    if(!_tableView){
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView = tableView;
        [self.view addSubview:_tableView];
        [tableView setDelegate:self];
        [tableView setDataSource:self];
        tableView.tag = 10;
        tableView.rowHeight = 80;
        tableView.separatorStyle = UITableViewCellSelectionStyleNone;
        [tableView registerNib:[UINib nibWithNibName:@"CKOrderStatusCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:statusReuseID];
    }
    return _tableView;
}
//订单详情
- (UITableView *)tableView2{
    if(!_tableView2){
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView2 = tableView;
        [self.view addSubview:_tableView2];
        [tableView setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
        [tableView setContentInset:UIEdgeInsetsMake(0, 0, 10, 0)];
        [tableView setDelegate:self];
        [tableView setDataSource:self];
        tableView.tag = 20;
        tableView.separatorStyle = UITableViewCellSelectionStyleNone;
        [tableView registerNib:[UINib nibWithNibName:@"CKOrderDetilCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:detailReuseID];
        [tableView registerNib:[UINib nibWithNibName:@"CKShouHuoCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:shouHuoReuseID];

    }
    return _tableView2;
}
- (UIView *)bottomView{
    if(!_bottomView){
        UIView *view = [[UIView alloc]init];
        _bottomView = view;
        [self.view addSubview:view];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 1)];
        [lineView setBackgroundColor: [UIColor lightGrayColor]];
        [view addSubview:lineView];
        
        UIButton *delBtn = [[UIButton alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 20, 10, -80, 30)];
        [view addSubview:delBtn];
        [delBtn setBackgroundImage:[UIImage imageNamed:@"v2_coupon_verify_normal"] forState:UIControlStateNormal];
        [delBtn setTitle:@"删除订单" forState:UIControlStateNormal];
        [delBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
        [delBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [delBtn addTarget:self action:@selector(delBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _bottomView;
}

- (void)delBtnClick:(UIButton *)sender{
    NSLog(@"删除订单 按钮点击事件");
}

- (UISegmentedControl *)myTitleView{
    if(!_myTitleView){
        NSString *str1 = self.viewType == kViewTypeMyOrder ? @"订单状态" : @"系统消息";
        NSString *str2 = self.viewType == kViewTypeMyOrder ? @"订单详情" : @"用户消息";
        
        UISegmentedControl *segmentView = [[UISegmentedControl alloc]initWithItems:@[str1,str2]];
        _myTitleView = segmentView;
        
        //设置titleView:
        [segmentView setFrame:CGRectMake(0, 0, 180, 26)];
        [segmentView setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor darkGrayColor]} forState:UIControlStateNormal];
        [segmentView setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} forState:UIControlStateSelected];
        [segmentView setTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"yellow1xs"]]];
        segmentView.selectedSegmentIndex = 0;
        self.navItem.titleView = segmentView;
        
        [segmentView addTarget:self action:@selector(segmentViewValueChanged:) forControlEvents:UIControlEventValueChanged];
        
    }
    return _myTitleView;
}
//切换订单状态/详细:
- (void)segmentViewValueChanged:(UISegmentedControl *)sender{

    if(sender.selectedSegmentIndex == 0){
        if(_tableView){
            [self.view bringSubviewToFront:self.tableView];
        }else{
            [self.view bringSubviewToFront:self.tableView3];
        }
    }else{
        if(_tableView2){
            [self.view bringSubviewToFront:self.tableView2];
        }else{
            [self.view bringSubviewToFront:self.tableView4];
        }
    }
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    UITableView *tableV1;
    UIView *tableV2;
    CGFloat bottomOffset;
    if(self.viewType == kViewTypeMyOrder){
        
        [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.bottom.trailing.equalTo(self.view);
            make.height.equalTo(@49);
        }];
        
        tableV2 = self.tableView2;
        tableV1 = self.tableView;
        bottomOffset = -49;
    }else if(self.viewType == kViewTypeMyMessage){
            [self.tableView4 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(self.view);
                make.top.equalTo(self.view).offset(64);
                make.leading.trailing.equalTo(self.view);
            }];
            
            UIImageView *imageV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"v2_my_message_empty"]];
            [self.tableView4 addSubview:imageV];
            [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(self.tableView4);
                make.centerY.equalTo(self.tableView4).offset(-70);
            }];
            
            
            UILabel *label = [[UILabel alloc]init];
            label.text = @"~~~并没有消息~~~";
            [label sizeToFit];
            [self.tableView4 addSubview:label];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.center.equalTo(self.tableView4);
            }];
        tableV1 = self.tableView3;
    }
    
    [tableV2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(64);
        make.bottom.equalTo(self.view).offset(-49);
        make.leading.trailing.equalTo(self.view);
    }];
    
    [tableV1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(64);
        make.bottom.equalTo(self.view).offset(bottomOffset);
        make.leading.trailing.equalTo(self.view);
    }];
    
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.myTitleView setBackgroundColor:[UIColor whiteColor]];
}

#pragma mark
#pragma mark tableView 数据源及代理方法:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if(tableView.tag == 30){
        return 5;
    }
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(tableView.tag == 20){
        return 4;
    }
    if(tableView.tag == 30){
        return 1;
    }
    
    NSArray *statusTimeline = [CKOrderModel sharedOrder].orderModelArray[self.row].status_timeline;
    return statusTimeline.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(tableView.tag == 20){
        if(indexPath.row == 0){
            tableView.rowHeight = 200;
            CKOrderDetilCell *cell = [tableView dequeueReusableCellWithIdentifier:detailReuseID forIndexPath:indexPath];
            
            return cell;
        }
        if(indexPath.row == 1){
            tableView.rowHeight = 121;
            CKShouHuoCell *cell = [tableView dequeueReusableCellWithIdentifier:shouHuoReuseID forIndexPath:indexPath];
            
            return cell;
        }
        if(indexPath.row == 2){
            tableView.rowHeight = 200;
            CKOrderDetilCell *cell = [tableView dequeueReusableCellWithIdentifier:detailReuseID forIndexPath:indexPath];
            
            return cell;
        }
        if(indexPath.row == 3){
            tableView.rowHeight = 121;
            CKShouHuoCell *cell = [tableView dequeueReusableCellWithIdentifier:shouHuoReuseID forIndexPath:indexPath];
            
            return cell;
        }
        
        
    }
    
    if(tableView.tag == 30){
        CKSystemMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:systemMessageCellID forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.tag = indexPath.section;
        
        //点击显示全部的回调方法:
        cell.showAllClickBlick = ^(CKSystemMessageCell *cell){
            
            
            NSNumber *num = cell.showAllBtn.isSelected ? @1 : @0;
            [self.heightArray replaceObjectAtIndex:cell.indexPath.section withObject:num];
            
            //动画更新tableView行高
            if(self.lastCell){
                [self.heightArray replaceObjectAtIndex:self.lastCell.indexPath.section withObject:@0];
                NSLog(@"收起");

            }else{
                [self.heightArray replaceObjectAtIndex:self.lastCell.indexPath.section withObject:@1];
                NSLog(@"展开");

            }
            [tableView beginUpdates];
            [tableView endUpdates];

            
            if(self.lastCell == cell){
                return;
            }
            
            [self.lastCell closeCell];
            
            self.lastCell = cell;
            
            
        };
        
        cell.tableView = tableView;
        cell.indexPath = indexPath;
        return cell;
    }
    
    CKOrderStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:statusReuseID forIndexPath:indexPath];
    
    CKStatusTimeline *model = [CKOrderModel sharedOrder].orderModelArray[self.row].status_timeline[indexPath.row];
    cell.status_timeline = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(tableView.tag == 30){
        
        NSNumber *num = self.heightArray[indexPath.section];
        if(num.integerValue){
            return 300;
        }
        return 100;
    }
    
    
    return tableView.rowHeight;
}




@end
